package com.zwr.sheller.sso.Service.impl;

import com.zwr.sheller.core.common.BusinessException;
import com.zwr.sheller.core.common.ExceptionCode;
import com.zwr.sheller.core.common.WQQBusinessException;
import com.zwr.sheller.core.common.WQQExceptionCode;
import com.zwr.sheller.core.pojo.User;
import com.zwr.sheller.core.util.JsonUtil;
import com.zwr.sheller.sso.Service.LoginService;
import com.zwr.sheller.sso.mapper.UserMapper;

import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 18032
 * @Date: 10:06 2022/1/11
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @Autowired(required = false)
    UserMapper userMapper;

    private static final String USER_TOKEN = "token:";
    @Override
    public Map<String,Object> login(String phone, String pwd) throws WQQBusinessException {
        Example example = new Example(User.class);
        Example.Criteria criteria1 = example.createCriteria();
        criteria1.andEqualTo("userPhone", phone).andEqualTo("userPwd", pwd);
        User user = userMapper.selectByExample(example).get(0);
        if (user == null){
            throw new WQQBusinessException(WQQExceptionCode.WRONG_PHONE_OR_PWD);
        }
        //添加token作为键到redis中
        UUID uuid = UUID.randomUUID();
        String token = uuid.toString().replace("-", "");
        redisTemplate.opsForValue().set(USER_TOKEN+token,user,30,TimeUnit.MINUTES);
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("user",user);
        objectObjectHashMap.put("token",token);
        return objectObjectHashMap;
    }

    @Override
    public User checkToken(String token) throws WQQBusinessException {
        Object o = redisTemplate.opsForValue().get(USER_TOKEN + token);
        String s = JsonUtil.objectToString(o);
        User user = JsonUtil.stringToObject(s, User.class);
        if (!StringUtils.isEmpty(o)){
            redisTemplate.expire(USER_TOKEN+token,30,TimeUnit.MINUTES);
            return user;
        }
        throw new WQQBusinessException(WQQExceptionCode.WRONG_TOKEN);
    }

    @Override
    public Integer forgetPwd(String phone, String code,String pwd) throws WQQBusinessException {
        //验证code是否正确
        Object o = redisTemplate.opsForValue().get(SmsServiceImpl.LOGIN_SMS + phone);
        if (StringUtils.isEmpty(o) || !o.equals(code)){
            throw new WQQBusinessException(WQQExceptionCode.WRONG_CODE);
        }
        User user = new User();
        user.setUserPhone(phone);
        User user1 = userMapper.select(user).get(0);
        if (StringUtils.isEmpty(user1)){
            throw new WQQBusinessException(WQQExceptionCode.NO_USER);
        }
        //修改密码
        user.setUserPwd(pwd);
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("userPhone",phone);
        int i = userMapper.updateByExampleSelective(user,example);
        if (i == 0){
            throw new WQQBusinessException(WQQExceptionCode.UPDATE_FAILED);
        }
        return i;
    }

    @Override
    public Integer addNewUser(User user,String code) throws WQQBusinessException {
        //1.判断该手机用户是否注册过
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("userPhone",user.getUserPhone());
        User user1 = userMapper.selectOneByExample(example);
        if (user1 != null){
            throw  new WQQBusinessException(WQQExceptionCode.USER_PHONE);
        }

        //验证码是否通过
        Object o = redisTemplate.opsForValue().get(SmsServiceImpl.LOGIN_SMS +user.getUserPhone());
        if (!code.equals(String.valueOf(o))){
            throw new WQQBusinessException(WQQExceptionCode.WRONG_CODE);
        }

        //注册用户
        User newUser = new User();
        newUser.setUserPhone(user.getUserPhone());
        newUser.setUserPwd(user.getUserPwd());
        newUser.setUserName(user.getUserName());
        newUser.setUserRetailName(user.getUserRetailName());
        newUser.setUserPrincipleName(user.getUserPrincipleName());
        newUser.setUserPrinciplePhone(user.getUserPrinciplePhone());
        int insert = userMapper.insert(user);
        if (insert == 0){
            throw new WQQBusinessException(WQQExceptionCode.INSERT_FAILED);
        }
        return insert;
    }

}
